#普通线性回归
from sklearn import linear_model, datasets
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error, mean_squared_error, explained_variance_score, r2_score
import numpy as np

#加载数据
x, y = datasets.make_regression(n_samples=100, n_features=1, noise=10)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
#加载模型
#线性回归模型
# reg = linear_model.LinearRegression()
# reg = linear_model.Ridge()
reg = linear_model.Lasso()
reg.fit(x_train, y_train)
#评估
pred_y = reg.predict(x_test)
#误差
print(mean_absolute_error(y_test, pred_y))
print(mean_squared_error(y_test, pred_y))
print(explained_variance_score(y_test, pred_y))
print(r2_score(y_test, pred_y))

_x = np.array([-2.5, 2.5])
_y = np.array(_x[:, None])
plt.scatter(x_test, y_test)
plt.plot(_x, _y, linewidth=3, color='red')
plt.show()